package com.muye.dianli;

import android.app.Application;
import android.app.Service;
import android.os.Vibrator;
import android.util.Log;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.BDNotifyListener;
import com.baidu.location.GeofenceClient;
import com.baidu.location.LocationClient;

public class MyApplication extends Application
{
	public LocationClient mLocationClient;
	
	public GeofenceClient mGeofenceClient;
	
	private MyLocationListener mMyLocationListener;
	
	public TextView mLocationResult;
	
	public Vibrator mVibrator;
	
	public NotifyLister mNotifyLister;
	
	@Override
	public void onCreate()
	{
		super.onCreate();
		mLocationClient = new LocationClient(this);
		mMyLocationListener = new MyLocationListener();
		mLocationClient.registerLocationListener(mMyLocationListener);
		
		mGeofenceClient = new GeofenceClient(this);
		mNotifyLister = new NotifyLister();
		mVibrator = (Vibrator) getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);
	}
	
	public class MyLocationListener implements BDLocationListener
	{
		
		@Override
		public void onReceiveLocation(BDLocation location)
		{
			// Receive Location
			StringBuffer sb = new StringBuffer(256);
			sb.append("time : ");
			sb.append(location.getTime());
			sb.append("\nerror code : ");
			sb.append(location.getLocType());
			sb.append("\nlatitude : ");
			sb.append(location.getLatitude());
			sb.append("\nlontitude : ");
			sb.append(location.getLongitude());
			sb.append("\nradius : ");
			sb.append(location.getRadius());
			if (location.getLocType() == BDLocation.TypeGpsLocation)
			{
				sb.append("\nspeed : ");
				sb.append(location.getSpeed());
				sb.append("\nsatellite : ");
				sb.append(location.getSatelliteNumber());
				sb.append("\ndirection : ");
				sb.append(location.getDirection());
			}
			else if (location.getLocType() == BDLocation.TypeNetWorkLocation)
			{
				sb.append("\naddr : ");
				sb.append(location.getAddrStr());
				sb.append("\noperationers : ");
				sb.append(location.getOperators());
			}
			logMsg(sb.toString());
			Log.i("BaiduLocationApiDem", sb.toString());
		}
		
		@Override
		public void onReceivePoi(BDLocation arg0)
		{
			
		}
	}
	
	public void logMsg(String str)
	{
		try
		{
			if (mLocationResult != null)
				mLocationResult.setText(str);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public class NotifyLister extends BDNotifyListener
	{
		public void onNotify(BDLocation mlocation, float distance)
		{
			mVibrator.vibrate(1000);
		}
	}
}
